<?php
/**
 * rail_documents are used to store binary files to rail. The files inserted as Blob to Databases.
 * A rail_document can have a lease one binary content. You can remove the old content or add a new one.
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-01-15
 * @package rail
 */
class rail_document extends rail_object {


	/**
	 * Copy rail_document.
	 *
	 * @exception rail_readpermission_exception
	 * @param rail_object $pRailObject
	 * @return integer
	 */
	public function copy ( $pRailObject ) {
		$copy = parent::copy( $pRailObject );

		$content = $this->get_content();
		$copy->set_content( $content, $this->get_mimetype() );

		return $copy;
	}


	/**
	 * get Blob content for this document. The returned String can be in binary or ASCII form.
	 *
	 * @exception rail_readpermission_exception
	 * @return Blob
	 */
	public function get_content () {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		if( !isset( $this->cache[ "content" ] ) ){
			$data = $this->rail_connector->send_request( "SELECT * FROM objects_documents WHERE `object`='" . $this->get_id() . "' LIMIT 1" )->fetch_assoc();
			$this->cache[ "mime-type" ] = $data[ "mime-type" ];
			$this->cache[ "content" ] = $data[ "content" ];
		}

		return $this->cache[ "content" ];
	}


	/**
	 * get mime-type for this document.
	 *
	 * @exception rail_readpermission_exception
	 * @return string
	 */
	public function get_mimetype () {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		if( !isset( $this->cache[ "mime-type" ] ) ){
			$data = $this->rail_connector->send_request( "SELECT `mime-type` FROM objects_documents WHERE `object`='" . $this->get_id() . "' LIMIT 1" )->fetch_assoc();
			$this->cache[ "mime-type" ] = $data[ "mime-type" ];
		}

		return $this->cache[ "mime-type" ];
	}


	/**
	 * return true if his rail_document has a content otherwise false.
	 *
	 * @exception rail_readpermission_exception
	 */
	public function is_content_set () {
		// check read permission
		if( !$this->is_readable( $this->rail_connector->get_user() ) )
		 	throw new rail_readpermission_exception( $this, $this->rail_connector->get_user() );

		$data = $this->rail_connector->send_request( "SELECT object FROM objects_documents WHERE `object`='" . $this->get_id() . "' LIMIT 1" )->fetch_assoc();

		// update content
		return $data[ "object" ] > 0;
	}


	/**
	 * remove content for this document.
	 *
	 * @exception rail_writepermission_exception
	 */
	public function remove_content () {
		// check write permission
		if( !$this->is_writeable( $this->rail_connector->get_user() ) )
		 	throw new rail_writepermission_exception( $this, $this->rail_connector->get_user() );

		$this->rail_connector->send_request( "DELETE FROM objects_documents WHERE object='" . $this->get_id() . "'" );
		unset( $this->cache[ "mime-type" ] );
		unset( $this->cache[ "content" ] );
	}


	/**
	 * set Blob content for this document. The content can be in binary or ASCII form.
	 *
	 * @exception rail_writepermission_exception
	 * @param Blob $pContent
	 * @param String $pMimeType
	 */
	public function set_content ( $pContent, $pMimeType = "text/plane" ) {
		// check write permission
		if( !$this->is_writeable( $this->rail_connector->get_user() ) )
		 	throw new rail_writepermission_exception( $this, $this->rail_connector->get_user() );

		// update content
		if( $this->is_content_set() )
			$this->rail_connector->send_request( "UPDATE objects_documents SET `mime-type`='$pMimeType', `content`='" . addslashes( $pContent ) . "' WHERE `object`='" . $this->get_id() . "'" );
		// insert content
		else
			$this->rail_connector->send_request( "INSERT INTO objects_documents (`object`,`mime-type`,`content`) VALUES ('" . $this->get_id() . "', '$pMimeType', '" . addslashes( $pContent ) . "')" );

		$this->cache[ "mime-type" ] = $pMimeType;
		$this->cache[ "content" ] = addslashes( $pContent );
	}

} // End of rail_document
?>